package org.e_gold.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.e_gold.dao.CookieDAO;
import org.e_gold.dto.Cookies;
import org.e_gold.service.TestWebService;
import org.e_gold.util.Constant;
import org.jsoup.Connection;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class TestWebServiceImpl extends _NavigatorService implements Constant, TestWebService {

    private static final Logger logger = Logger.getLogger(EventServiceImpl.class);
    Map<String, String> headers = null;
    String location = "";
    Cookies cookies = new Cookies();

    @Autowired
    private CookieDAO cookieDAO;

    private Cookies initBasicHeaders() {

        try {
            redirectNavigator(HOME_URL);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cookies;
    }

    @Override
    public void start() {

        try {
            headers = prepareBasicHeaders();
            if (cookieDAO.findAll() != null && cookieDAO.findAll().size() > 0) {
                // re-store cookie for logined session or basic session for the first time
                if (cookies.getCookies() == null) {
                    cookies.setCookies(cookieDAO.findAll());
                }
                headers.put(COOKIE, cookies.getCookie());
            } else {
                cookies = initBasicHeaders();
            }
            // go to In-Play -> Soccer
            Document soccers = redirectNavigator(SOCCER_IN_PLAY_URL);
            Elements events = soccers.select("div.Level2");
            logger.info(":::::::::::::::");
            logger.info(":::::::::::::::");
            int matchNum = events.size() - 3;
            for (int i = 0; i < matchNum; i++) {
                Element e = events.get(i);
                logger.info(e);
            }
            logger.info(":::::::::::::::");
            logger.info(":::::::::::::::");
            // if (!browserService.isLogined(cookies, headers)) {
            // loginAccount();
            // }
            // echoBalance();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @SuppressWarnings("unused")
    private void loginAccount() {

        Connection c = null;
        try {
            c = navigator(LOGIN_URL);
            Document loginPage = c.get();
            Element loginForm = loginPage.getElementById("aspnetForm");
            // prepare login data
            Map<String, String> loginData = prepareLoginData(loginForm);
            Document loginedPage = postForm(BASE_URL + loginForm.attr("action"), loginData);
            logger.info(loginedPage);
        } catch (Exception e) {
            logger.error(e);
        }
    }

    private Map<String, String> prepareLoginData(Element loginForm) {

        Map<String, String> loginData = new HashMap<String, String>();
        loginData.put("txtUsername", "lamhanh86");
        loginData.put("txtPassword", "qwer1234");
        for (Element e : loginForm.select("input[type=hidden]")) {
            loginData.put(e.attr("name"), e.attr("value"));
        }
        headers.put(CONTENT_TYPE, CONTENT_TYPE_DATA);
        headers.put(ORIGIN, "");
        headers.put(COOKIE, cookies.getCookie());
        return loginData;
    }

    public static void main(String args[]) {

        new TestWebServiceImpl().start();
    }
}
